package com.itheima.service;

import com.itheima.api.OrderService;
import com.itheima.util.IpUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.beans.factory.annotation.Value;

import java.util.concurrent.TimeUnit;

/**
 * @description
 * @author: ts
 */
@DubboService(
        //group = "v1"
        //,version = "v1"
)
public class OrderServiceImpl implements OrderService {


    @Value("${dubbo.protocol.port}")
    private String rpcServerPort;


    @Override
    public String getOrder(Long orderId) {
        String result = IpUtil.getRealIp()+" ,orderId="+orderId +",rpcServerPort="+rpcServerPort;
        System.out.println(Thread.currentThread().getName() +"---" + result);

        if (orderId == -1L) {
            try {
                System.out.println("waiting for 2 seconds...");
                TimeUnit.SECONDS.sleep(2);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (orderId == 0L) {
            System.out.println("请降级");
            throw new RpcException("请降级");
        }
        return result;
    }
}
